package com.xbongbong.workflow.service.strategy.data.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.formdata.pojo.SaasNeedRedundantAttrPojo;
import com.xbongbong.pro.message.constant.CommunicatePushConstant;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.PushRefTypePojo;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.service.MessageService;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.base.CommunicateBaseEnum;
import com.xbongbong.saas.enums.business.CustomerCommunicateEnum;
import com.xbongbong.saas.enums.business.SupplierCommunicateEnum;
import com.xbongbong.saas.enums.business.SupplierEnum;
import com.xbongbong.saas.model.SupplierModel;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.workflow.pojo.Data;
import com.xbongbong.workflow.pojo.User;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;
import com.xbongbong.workflow.service.strategy.data.AbstractWorkflowDataStrategy;
import com.xbongbong.workflow.service.strategy.data.WorkflowDataProcessStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 *
 */
@Service
@Slf4j
public class WorkflowSupplierCommunicateStrategy extends AbstractWorkflowDataStrategy implements WorkflowDataProcessStrategy {

    @Resource
    private SupplierModel supplierModel;
    @Resource
    private MessageService messageService;

    @Override
    public List<Integer> type() {
        return Collections.singletonList(XbbRefTypeEnum.SUPPLIER_COMMUNICATE.getCode());
    }

    @Override
    public void save(WorkflowTransferPOJO workflowTransferPOJO, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) throws XbbException {

        // 文件柜
        saveDataCabinetFile(workflowTransferPOJO);

        String corpid = workflowTransferPOJO.getCorpid();
        Data pojoData = workflowTransferPOJO.getData();
        Long dataId = pojoData.getId();
        JSONObject data = pojoData.getData();
        User user = workflowTransferPOJO.getUser();

        Long supplierId = FastJsonHelper.getLongOrDefaultFromFormData(data, SupplierCommunicateEnum.SUPPLIER_ID.getAttr(), 0L);
        Long communicateTime = FastJsonHelper.getLongOrDefaultFromFormData(data, SupplierCommunicateEnum.COMMUNICATE_TIME.getAttr(), 0L);
        PaasFormDataEntityExt supplierEntityExt = supplierModel.getByKey(supplierId, corpid);
        Long lastConnectTime = FastJsonHelper.getLongOrDefaultFromFormData(supplierEntityExt.getData(), SupplierEnum.LAST_CONNECT_TIME.getAttr(), 0L);
        if (lastConnectTime < communicateTime){
            if (communicateTime > DateTimeUtil.getInt()) {
                communicateTime = DateTimeUtil.getInt();
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(SupplierEnum.LAST_CONNECT_TIME.getAttr(), communicateTime);
            UpdateDataEntity updateData = ExplainUtil.getUpdateData(supplierId, jsonObject, corpid);
            supplierModel.updateBatch(Collections.singletonList(updateData), corpid);
        }
        // 供应商跟进记录@指定人员
        List<String> atUserIds = saasNeedRedundantAttrPojo.getAtUserIds();
        if (Objects.isNull(atUserIds)) {
            JSONArray jsonArray = data.getJSONArray(CustomerCommunicateEnum.AT_USERNAME.getAttr());
            if (Objects.nonNull(jsonArray)) {
                atUserIds = JSONArray.parseArray(jsonArray.toJSONString(),String.class);
            }
        }
        if (Objects.nonNull(atUserIds) && atUserIds.size() > 0) {
            atUserIds.remove(user.getUserId());

            String pushTitle = I18nMessageUtil.getMessage(CommunicatePushConstant.COMMUNICATE_AT_TITLE);
            String customerName = FastJsonHelper.getStringOrDefaultFromFormData(data, CommunicateBaseEnum.MAIN_BUSINESS_HIDE.getAttr(), "");
            String pushContent = String.format(I18nMessageUtil.getMessage(CommunicatePushConstant.COMMUNICATE_AT_CONTENT), user.getName(), customerName);

            PushTypeEnum pushTypeEnum = PushTypeEnum.COMMUNICATE_ADD_PUSH;

            PushRefTypePojo.Options options = new PushRefTypePojo.Options(pojoData.getAppId(), pojoData.getMenuId(), pojoData.getFormId(), dataId, Collections.singletonList(dataId), SaasMarkEnum.SAAS.getCode(), pojoData.getBusinessType(), pojoData.getBusinessType());
            MessageRabbitMqDTO messageRabbitMqDTO = new MessageRabbitMqDTO(corpid, atUserIds, Collections.singletonList(dataId), 0, null, pushTitle, pushContent, null, options);
            messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, pushTypeEnum);
        }

        // api
        apiHook(workflowTransferPOJO);
        addLinkDataDynamic(workflowTransferPOJO);
        // log
        afterSaveLog(workflowTransferPOJO);
    }

    @Override
    public void update(WorkflowTransferPOJO workflowTransferPOJO, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) throws XbbException {
       save(workflowTransferPOJO, saasNeedRedundantAttrPojo);
       // log
        afterUpdateLog(workflowTransferPOJO);
    }

    @Override
    public void updateBack(Long taskId, WorkflowTransferPOJO workflowTransferPOJO) throws XbbException {
        // 没有审批
    }

    @Override
    public void approvalFailed(Long taskId, WorkflowTransferPOJO workflowTransferPOJO, Integer taskType) throws XbbException {
        // 没有审批
    }


}
